package com.lixingqu.mvpdemo.ui.login;


import android.os.Handler;

/**
 * Created by lixingqu on 2016/11/28.
 */

public class LoginPresenterImpl implements ILoginPresenter {

    private ILoginView mView;
    private ILoginModel mModel;
    private Handler mHandler = new Handler();

    public LoginPresenterImpl(ILoginView mView) {
        this.mView = mView;
        mModel = new LoginModelImpl();
    }

    @Override
    public void login() {
        mView.onRequestStart();
        mModel.login(mView.getUsername(), mView.getPassword(), new LoginModelImpl.Callback() {
            @Override
            public void loginSuccess(final User user) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.onRequestEnd();
                        mView.loginSuccess(user);
                    }
                });
            }

            @Override
            public void loginError() {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.onRequestError();
                    }
                });
            }
        });
    }

    @Override
    public void reset() {
        mView.reset();
    }
}
